package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2019/4/30.<br><br>
 *
 * 只出现一次的数字 II
 */
public class Topic137 {

    public static void main(String[] args) {
        Topic137 instance = new Topic137();

        int[] array = {2, 2, 3, 2};
        System.out.println(instance.singleNumber(array));

        array = new int[] {0, 1, 0, 1, 0, 1, 99};
        System.out.println(instance.singleNumber(array));
    }

    public int singleNumber(int[] nums) {
        int x1 = 0;
        int x2 = 0;
        int mask;

        for(int value : nums) {
            x2 ^= x1 & value;
            x1 ^= value;

            mask = ~(x2 & x1);

            x2 &= mask;
            x1 &= mask;
        }

        return x1;
    }
}
